#include <string>
#include <vector>
#include <list>
#include <set>
#include <algorithm>
#include <iostream>
#include <functional>
using namespace std;
template <typename T>
//函数
void print_v(vector<T> &v)
{
	for (vector<T>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
}
//函数
template <typename T>
void show_vector(T &t)
{
	cout << t << " ";
}

//仿函数（函数对象）
template <typename T>
class ShowV
{
public:
	int i = 0;
	void operator()(T &t)
	{
		i++;
		cout << t << " ";
	}
	void print_i()
	{
		cout << "i = " << i << endl;
	}
	ShowV() {}
	~ShowV()
	{
		cout << "调用了析构函数" << endl;
	}
	ShowV(const ShowV & showV)
	{
		this->i = showV.i;
		cout << "调用了拷贝构造函数" << endl;
	}
};
//for_each算法
void my_for_each()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(4);
	//print_v(v);
	//for_each(v.begin(), v.end(), show_vector<int>);
	ShowV<int> show_v = ShowV<int>();
	//show_v是值传递,遍历完成后返回该对象，调用了两次拷贝构造函数
	ShowV<int> result = for_each(v.begin(), v.end(), show_v);
	result.print_i();//i = 3
	show_v.print_i();//i = 0 因为是值传递,实参不会改变

}
int main3()
{
	my_for_each();
	system("pause");
	return 0;
}